package com.koron.common.web.controller.message;

import com.koron.bean.base.Response;
import com.koron.common.core.util.message.MessageUtil;
import com.koron.common.web.pojo.MessageBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName MessageSendController
 * @description
 * @Author zhouj
 * @Date 2022/3/21 15:41
 */
@Api(tags = "发送短信测试(messageSendController)")
@RestController
@RequestMapping("/messageUtil/")
@Slf4j
public class MessageSendController {

    @Autowired
    MessageUtil messageUtil;

    @ApiOperation(value = "测试发送消息获取链接")
    @GetMapping("getUrl")
    public Response<String> getUrl(String businessCode, String... args) {
        //messageUtil.sendQywxText("krzhoujun","测试企业微信");
        //messageUtil.sendSms("18873269565","测试短信");
        String qywxHref = messageUtil.generateQywxHref(businessCode, args);
        return Response.ok(qywxHref);
    }

    @ApiOperation(value = "测试发送消息")
    @GetMapping("send")
    public Response<String> testMessage(String topicCode, String qywxHref, String handler, String... args) {
        messageUtil.sendMessageWithTemplate(topicCode, qywxHref, handler, args);
        return Response.ok(qywxHref);
    }

    @ApiOperation(value = "发送短信和企业微信")
    @PostMapping("sendMessage")
    public Response<String> sendMessage(@RequestBody MessageBody messageBody) {
        log.info("发送消息：messageBody: " + messageBody);
        Assert.notEmpty(messageBody.getNotifyType(), "notifyType must not be empty!");
        if (messageBody.getNotifyType().length > 0) {
            for (int i = 0; i < messageBody.getNotifyType().length; i++) {
                messageUtil.sendMessageWithTemplate(messageBody.getTopicCode(), messageBody.getUrl(), messageBody.getHandler(), messageBody.getArgs());
            }
        }
        return Response.ok();
    }

}
